﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace SDS.SPHealthAnalyzerRules.Delegation
{
    //[Guid("320ECDF8-FAF1-4127-80CF-E61D6BA8945E")]
    public class LoggingService : Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase
    {
        private const string LoggingServiceName = "Delegation Health Rules Logging Service";
        private const string LoggingAreaName = "Delegation Health Rules";

        // Local accessible instance
        public static LoggingService Local
        {
            get
            {
                IEnumerable<LoggingService> LogSvcs = SPFarm.Local.Services.OfType<LoggingService>();

                if (LogSvcs.Count() == 1)
                    return LogSvcs.Single();

                // If the service is registered multiple times, remove any duplicates
                if (LogSvcs.Count() > 1)
                    Unregister();

                // instantiate and register the new instance
                LoggingService svc = new LoggingService();
                svc.Update();

                return svc;
            }
        }

        // Constructor
        public LoggingService() : base(LoggingServiceName, SPFarm.Local) { }

        // overrides
        public override string DisplayName { get { return LoggingAreaName; } }
        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas() { yield return Area; }
        public static void Write(SPDiagnosticsCategory Category, TraceSeverity Severity, string ErrorMessage, Guid CorrelationToken)
        {
            //LoggingService.Current.WriteTrace(0, Category, Severity, String.Format("{0}\t{1}", ErrorMessage, CorrelationToken.ToString()));
            //SPDiagnosticsService.Local.WriteTrace(0, Category, Severity, String.Format("{0}\t{1}", ErrorMessage, CorrelationToken.ToString()));
            Local.WriteTrace(0, Category, Severity, String.Format("{0}\t{1}", ErrorMessage, CorrelationToken.ToString()));
        }


        public static void Unregister()
        {
            SPFarm.Local.Services
                        .OfType<LoggingService>()
                        .ToList()
                        .ForEach(s =>
                        {
                            s.Delete();
                            s.Unprovision();
                            s.Uncache();
                        });
        }



        private SPDiagnosticsArea Area
        {
            get
            {
                return new SPDiagnosticsArea(
                    LoggingAreaName,
                    new List<SPDiagnosticsCategory>()
                    {
                        Category_FeatureReceiver,
                        Category_C2WTS,
                        Category_WebApp,
                        Category_BCS,
                        Category_Excel,
                        Category_Visio,
                        Category_PerformancePoint,
                        Category_PowerPivot,
                        Category_SSRS
                    }
                );
            }
        }
        #region #####   CATEGORIES   #####

        public static SPDiagnosticsCategory Category_FeatureReceiver
        { get { return new SPDiagnosticsCategory("Health Analyzer Rules Feature", TraceSeverity.Medium, EventSeverity.Information); } }

        public static SPDiagnosticsCategory Category_C2WTS
        { get { return new SPDiagnosticsCategory("Svc: Claims to Windows Token Service", TraceSeverity.Medium, EventSeverity.Information); } }

        public static SPDiagnosticsCategory Category_WebApp
        { get { return new SPDiagnosticsCategory("Web Application", TraceSeverity.Medium, EventSeverity.Information); } }

        public static SPDiagnosticsCategory Category_BCS
        { get { return new SPDiagnosticsCategory("Svc: Business Connectivity Services", TraceSeverity.Medium, EventSeverity.Information); } }

        public static SPDiagnosticsCategory Category_Excel
        { get { return new SPDiagnosticsCategory("Svc: Excel Services", TraceSeverity.Medium, EventSeverity.Information); } }

        public static SPDiagnosticsCategory Category_Visio
        { get { return new SPDiagnosticsCategory("Svc: Visio Services", TraceSeverity.Medium, EventSeverity.Information); } }

        public static SPDiagnosticsCategory Category_PerformancePoint
        { get { return new SPDiagnosticsCategory("Svc: PerformancePoint Services", TraceSeverity.Medium, EventSeverity.Information); } }

        public static SPDiagnosticsCategory Category_PowerPivot
        { get { return new SPDiagnosticsCategory("Svc: PowerPivot", TraceSeverity.Medium, EventSeverity.Information); } }

        public static SPDiagnosticsCategory Category_SSRS
        { get { return new SPDiagnosticsCategory("Svc: SQL Server Reporting Services", TraceSeverity.Medium, EventSeverity.Information); } }

        #endregion

    }
}
